package com.zxyankh.common.rpc.client;

import com.zxyankh.common.rpc.serialize.MsgpackDecoder;
import com.zxyankh.common.rpc.serialize.MsgpackEncoder;
import io.netty.channel.Channel;
import io.netty.channel.pool.ChannelPoolHandler;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.LengthFieldPrepender;

/**
 * @author ankh
 * @since 23.11.2017
 */
public class ClientConnPool implements ChannelPoolHandler {

    @Override
    public void channelReleased(Channel ch) throws Exception {
    }

    @Override
    public void channelAcquired(Channel ch) throws Exception {
    }

    @Override
    public void channelCreated(Channel ch) throws Exception {
        SocketChannel socketChannel = (SocketChannel) ch;
        socketChannel.config().setKeepAlive(true);
        socketChannel.config().setTcpNoDelay(true);
        socketChannel.pipeline()
                .addLast("frameDecoder", new LengthFieldBasedFrameDecoder(65535, 0, 2, 0, 2))
                .addLast("msgpack decoer", new MsgpackDecoder())
                .addLast("frameEncoder", new LengthFieldPrepender(2))
                .addLast("msgpack encoder", new MsgpackEncoder());
    }
}
